package cn.net.sinodata.system.controller.system;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.net.sinodata.common.log.annotation.Log;
import cn.net.sinodata.common.web.core.BaseController;
import cn.net.sinodata.common.mybatis.core.page.PageQuery;
import cn.net.sinodata.common.core.domain.R;
import cn.net.sinodata.common.core.validate.AddGroup;
import cn.net.sinodata.common.core.validate.EditGroup;
import cn.net.sinodata.common.log.enums.BusinessType;
import cn.net.sinodata.common.excel.utils.ExcelUtil;
import cn.net.sinodata.system.domain.vo.BmTaskLogVo;
import cn.net.sinodata.system.domain.bo.BmTaskLogBo;
import cn.net.sinodata.system.service.IBmTaskLogService;
import cn.net.sinodata.common.mybatis.core.page.TableDataInfo;

/**
 * 定时任务日志
 *
 * @author chenbaijun
 * @date 2025-10-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/taskLog")
public class BmTaskLogController extends BaseController {

    private final IBmTaskLogService bmTaskLogService;

    /**
     * 查询定时任务日志列表
     */
    @GetMapping("/list")
    public TableDataInfo<BmTaskLogVo> list(BmTaskLogBo bo, PageQuery pageQuery) {
        return bmTaskLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出定时任务日志列表
     */
    @Log(title = "定时任务日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BmTaskLogBo bo, HttpServletResponse response) {
        List<BmTaskLogVo> list = bmTaskLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "定时任务日志", BmTaskLogVo.class, response);
    }

    /**
     * 获取定时任务日志详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<BmTaskLogVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(bmTaskLogService.queryById(id));
    }

    /**
     * 新增定时任务日志
     */
    @Log(title = "定时任务日志", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BmTaskLogBo bo) {
        return toAjax(bmTaskLogService.insertByBo(bo));
    }

    /**
     * 修改定时任务日志
     */
    @Log(title = "定时任务日志", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BmTaskLogBo bo) {
        return toAjax(bmTaskLogService.updateByBo(bo));
    }

    /**
     * 删除定时任务日志
     *
     * @param ids 主键串
     */
    @Log(title = "定时任务日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(bmTaskLogService.deleteWithValidByIds(List.of(ids), true));
    }

}
